<?php
class RestController extends Controller
{
	public function actionList() {
		switch ($_GET['model']) {
			case 'comisarias':
				$data = Comisarias::model()->findAll();
				$results = array();
				foreach ($data as $datum) {
					$r = array();
					$r['type'] = 'Feature';
					$r['properties'] = array();
					$r['properties']['nombre'] = $datum->referencia;
					$r['properties']['id']     = $datum->id;
					$r['geometry'] = array();
					$geometry = CJSON::decode($datum->GeoJSON);
					$r['geometry']['type']        = $geometry['type'];
					$r['geometry']['coordinates'] = $geometry['coordinates'];
					$r['center']['lat']   = $datum->centroid[0];
					$r['center']['lon']   = $datum->centroid[1];
					$results[] = $r;
				}
				echo CJSON::encode($results);
				break;
			case 'puntos':
				$data = Points::model()->findAll();
				$results = array();
				foreach ($data as $datum) {
					$r = array();
					$r['type'] = 'Feature';
					$r['properties'] = array();
					$r['properties']['type'] = $datum->type;
					$r['properties']['gid']     = $datum->gid;
					$r['properties']['type']     = $datum->type;
					$r['geometry'] = array();
					$geometry = CJSON::decode($datum->GeoJSON);
					$r['geometry']['type']        = $geometry['type'];
					$r['geometry']['coordinates'] = $geometry['coordinates'];
					$r['center']['lat']   = $datum->centroid[0];
					$r['center']['lon']   = $datum->centroid[1];
					$results[] = $r;
				}
				echo CJSON::encode($results);
				break;
			case 'jurisdiccion':
				$data = Jurisdiccion::model()->findAll();
				$results = array();
				foreach ($data as $datum) {
					$r['type'] = 'Feature';
					$r['properties'] = array();
					$r['geometry'] = array();
					
					$connection = Yii::app()->dblima;
					$sql_command = "SELECT ST_AsGeoJSON('" . $datum->geom . "')";
					$command = $connection->createCommand($sql_command);
					$data = $command->queryRow();
					$data = $data['st_asgeojson'];
					$geometry = CJSON::decode($data);
					$r['geometry']['type']        = $geometry['type'];
					$r['geometry']['coordinates'] = $geometry['coordinates'];
					$results[] = $r;
				}
				echo CJSON::encode($results);
		}
	}
	public function actionCreate() {
		switch($_GET['model']) {
			case 'jurisdiccion':
				$json = file_get_contents('php://input'); 
				$vars = CJSON::decode($json, true);

				$connection = Yii::app()->dblima;
				$sql_command = "SELECT ST_GeomFromText('" . $vars['data'] . "')";
				$command = $connection->createCommand($sql_command);
				$data = $command->queryRow();
				$model = new Jurisdiccion;
				$model->lima_comisaria_id = $vars['comisaria_id'];
				$model->geom = $data['st_geomfromtext'];
				$model->save();
				break;
			case 'puntos':
				$json = file_get_contents('php://input'); 
				$vars = CJSON::decode($json, true);

				$connection = Yii::app()->dblima;
				$sql_command = "SELECT ST_GeomFromText('" . $vars['data'] . "')";
				$command = $connection->createCommand($sql_command);
				$data = $command->queryRow();
				$model = new Points;
				$model->type = $vars['type'];
				$model->geom = $data['st_geomfromtext'];
				$model->save();
				break;
		}
	}

}